package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善用户信息
func (l *UserPerfectLogic) UserPerfect(in *zhihurpc.UserPerfectRequest) (*zhihurpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Where("id = ?", in.Id).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在，无法完善信息")
	}

	users := mysql.User{
		Model:   gorm.Model{ID: uint(in.Id)},
		Name:    in.Name,
		Image:   in.Image,
		Sex:     in.Sex,
		Info:    in.Info,
		Address: in.Address,
	}
	mysql.DB.Debug().Updates(&users)
	return &zhihurpc.UserPerfectResponse{
		Id: int64(users.ID),
	}, nil
}
